package com.by.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.by.entity.Attachment;
import com.by.entity.Category;
import com.by.entity.Goods;
import com.by.mapper.GoodsMapper;
import com.by.service.AttachmentService;
import com.by.service.CategoryService;
import com.by.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 黄佳辉
 * @since 2023-01-04
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public Boolean saveWithPic(Goods goods) {

        boolean saveResult = this.save(goods);
        if (saveResult) {
            Attachment attachment = new Attachment();
            attachment.setUrl(goods.getUrl());
            attachment.setBusinessId(goods.getId());
            attachmentService.save(attachment);
        }
        return saveResult;
    }

    @Override
    public Page<Goods> queryByPage(Integer page, Integer pageSize) {
        Page<Goods> goodsPage = null;
        if (page != null) goodsPage = new Page<>(page, pageSize);
        else goodsPage = new Page<>();
        List<Goods> records = this.page(goodsPage).getRecords();
        List<Goods> newRecords = records.stream().map((item) -> {
            // 赋值url
            String goodsId = item.getId();
            LambdaQueryWrapper<Attachment> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Attachment::getBusinessId, goodsId);
            Attachment attachment = attachmentService.getOne(wrapper);
            item.setUrl(attachment.getUrl());
            // 赋值categoryPath
            Long categoryId = item.getCategoryId();
            if (categoryId != null) {
                Long[] path = this.findParentId(categoryId);
                item.setCategoryPath(path);
            }
            return item;
        }).collect(Collectors.toList());
        goodsPage.setRecords(newRecords);
        return goodsPage;
    }

    // 查询父id列表
    private Long[] findParentId(Long categoryId) {
        List<Long> result = new ArrayList<>();
        int count = 0;
        while (!categoryId.equals(0L)) {
            Category categoryById = categoryService.getById(categoryId);
            result.add(categoryById.getId());
            categoryId = categoryById.getParentId();
            count++;
        }
        Collections.reverse(result);
        Long[] longs = new Long[count];
        return result.toArray(longs);
    }

    @Override
    public Boolean updateWithPic(Goods goods) {
        boolean updateResult = this.updateById(goods);

        String goodsId = goods.getId();
        LambdaUpdateWrapper<Attachment> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Attachment::getBusinessId, goodsId);
        wrapper.set(Attachment::getUrl, goods.getUrl());
        attachmentService.update(wrapper);

        return updateResult;

    }
}
